package com.village.zpark.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.village.zpark.mapper.CadresMapper;
import com.village.zpark.mapper.ClinicMapper;
import com.village.zpark.mapper.FarmerMapper;
import com.village.zpark.pojo.Clinic;
import com.village.zpark.pojo.Farmer;
import com.village.zpark.service.ClinicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
@Transactional
public class ClinicServiceImpl implements ClinicService {
    @Autowired
    private ClinicMapper clinicMapper;
    @Override
    public List<Clinic> findAll() {
        return clinicMapper.selectAll();
    }

    @Override
    public Clinic findById(Integer id) {
        return clinicMapper.selectByPrimaryKey(id);
    }

    @Override
    public int addClinic(Clinic clinic) {
        return clinicMapper.insert(clinic);
    }

    @Override
    public int updateClinic(Clinic clinic) {
        return clinicMapper.updateByPrimaryKey(clinic);
    }

    @Override
    public int deleteClinicById(Integer id) {
        return clinicMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo<Clinic> findPage(int page, int size, String param) {
        PageHelper.startPage(page, size);
        Example example = new Example(Clinic.class);
        Example.Criteria criteria = example.createCriteria();
        if (param != null && !param.isEmpty()) {
            criteria.orLike("clinicname", "%" + param + "%")
                    .orLike("clinicaddress", "%" + param + "%")
                    .orLike("contactphone", "%" + param + "%"); // 使用 orLike 来实现多字段模糊查询
        }
        List<Clinic> clinics;
        if (param != null && !param.isEmpty()) {
            clinics = clinicMapper.selectByExample(example);
        } else {
            clinics = clinicMapper.selectAll(); // 当搜索栏为空时，返回全部表单信息
        }
        return new PageInfo<>(clinics);
    }
}
